package com.weavernorth;

import com.weavernorth.sysap.util.CommonToSapUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import java.util.Calendar;


public class GoodsAction implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        //获取当前日期
        Calendar now = Calendar.getInstance();
        int day=now.get(Calendar.DAY_OF_MONTH);
        String requestid = requestInfo.getRequestid();//请求ID
        String workflowid = requestInfo.getWorkflowid();//流程ID
        String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
        bb.writeLog("tablename的值是:" + tablename);
        //1.获取表单数据
        String sql = " select  * from " + tablename + " where requestid =" + requestid;

        if(day>=18&&day<=23){
         String sqlCheck="select requestid,workflowid from "+tablename +"  fm,workflow_requestase wr where fm.requestid=wr.requestid and wr.currentnodetype!=0 and fm.cglx=0  and SUBSTRING(fm.sqsj,0,6)=(select SUBSTRING(sqsj,0,6)from "+tablename+" where requestid='"+requestid+"')";
         rs.execute(sqlCheck);
         while(rs.next()){

         }
            //2.封装sap请求xml
            StringBuffer  xml=new StringBuffer("<requesid>"+requestid+"</requesid>");
            xml.append("<workflowid>"+rs.getString("workflowid")+"</workflowid>");
            CommonToSapUtil commonToSapUtil=new CommonToSapUtil();
            try {
                //3.推送数据
                String responseXml=   commonToSapUtil.sendPost("url", xml.toString(), "utf-8");
                String strWorkcode="1234";
               //4.如果需要更新表单字段
                String sqlIp="update "+tablename+" set hth="+strWorkcode+" where requestid="+requestid;
                rs.executeQuery(sqlIp);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(rs.getCounts()>1){
             requestInfo.getRequestManager().setMessagecontent("申请日不在允许日期内");
             return FAILURE_AND_CONTINUE;
         }else{
             return SUCCESS;
         }
        }else {
            requestInfo.getRequestManager().setMessagecontent("申请日不在允许日期内");
            return FAILURE_AND_CONTINUE;
        }

    }
}



